/*     */ package com.zimbra.cs.ldap.unboundid;
/*     */ 
/*     */ import com.unboundid.ldap.sdk.BindRequest;
/*     */ import com.unboundid.ldap.sdk.GetEntryLDAPConnectionPoolHealthCheck;
/*     */ import com.unboundid.ldap.sdk.LDAPConnection;
/*     */ import com.unboundid.ldap.sdk.LDAPConnectionPool;
/*     */ import com.unboundid.ldap.sdk.LDAPException;
/*     */ import com.unboundid.ldap.sdk.PostConnectProcessor;
/*     */ import com.unboundid.ldap.sdk.ServerSet;
/*     */ import com.unboundid.ldap.sdk.SimpleBindRequest;
/*     */ import com.unboundid.ldap.sdk.StartTLSPostConnectProcessor;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.cs.ldap.LdapConnType;
/*     */ import com.zimbra.cs.ldap.LdapException;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig.ConnPoolKey;
/*     */ import com.zimbra.cs.ldap.LdapTODO.TODO;
/*     */ import java.io.PrintStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import javax.net.ssl.SSLContext;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class LdapConnectionPool
/*     */ {
/*     */   public static final String CP_ZIMBRA_REPLICA = "ZimbraReplica";
/*     */   public static final String CP_ZIMBRA_MASTER = "ZimbraMaster";
/*  52 */   private static final Map<String, LDAPConnectionPool> connPools = new HashMap();
/*     */   
/*     */ 
/*     */   static LDAPConnectionPool createConnectionPool(String connPoolName, LdapServerConfig config)
/*     */     throws LdapException
/*     */   {
/*  58 */     LDAPConnectionPool connPool = null;
/*     */     
/*  60 */     if (InMemoryLdapServer.isOn()) {
/*  61 */       connPool = createConnPoolToInMemoryLdapServer(config);
/*     */     } else {
/*  63 */       connPool = createConnPool(config);
/*     */     }
/*     */     
/*  66 */     connPool.setConnectionPoolName(connPoolName);
/*  67 */     connPool.setMaxWaitTimeMillis(config.getConnPoolTimeoutMillis());
/*     */     
/*  69 */     boolean onCheckoutHealthCheckEnabled = config.isConnPoolHelathCheckOnCheckoutEnabled();
/*  70 */     boolean backgroundHealthCheckEnabled = !onCheckoutHealthCheckEnabled;
/*     */     
/*     */ 
/*     */ 
/*  74 */     if (backgroundHealthCheckEnabled) {
/*  75 */       connPool.setHealthCheckIntervalMillis(config.getConnPoolHelathCheckBackgroundIntervalMillis());
/*     */     }
/*     */     
/*     */ 
/*  79 */     GetEntryLDAPConnectionPoolHealthCheck healthChecker = new GetEntryLDAPConnectionPoolHealthCheck(null, config.getConnPoolHelathCheckMaxResponseTimeMillis(), false, onCheckoutHealthCheckEnabled, false, backgroundHealthCheckEnabled, false);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  89 */     connPool.setHealthCheck(healthChecker);
/*     */     
/*  91 */     addToPoolMap(connPool);
/*     */     
/*  93 */     return connPool;
/*     */   }
/*     */   
/*     */   private static LDAPConnectionPool createConnPool(LdapServerConfig config) throws LdapException
/*     */   {
/*  98 */     LdapServerPool serverPool = new LdapServerPool(config);
/*     */     
/* 100 */     ServerSet serverSet = serverPool.getServerSet();
/* 101 */     BindRequest bindRequest = createBindRequest(config);
/*     */     
/* 103 */     PostConnectProcessor postConnectProcessor = null;
/* 104 */     if (serverPool.getConnectionType() == LdapConnType.STARTTLS) {
/* 105 */       SSLContext startTLSContext = LdapSSLUtil.createSSLContext(config.sslAllowUntrustedCerts());
/*     */       
/* 107 */       postConnectProcessor = new StartTLSPostConnectProcessor(startTLSContext);
/*     */     }
/*     */     
/* 110 */     LDAPConnectionPool connPool = null;
/*     */     try {
/* 112 */       connPool = new LDAPConnectionPool(serverSet, bindRequest, config.getConnPoolInitSize(), config.getConnPoolMaxSize(), postConnectProcessor);
/*     */       
/*     */ 
/* 115 */       connPool.setRetryFailedOperationsDueToInvalidConnections(true);
/*     */     } catch (LDAPException e) {
/* 117 */       throw UBIDLdapException.mapToLdapException(e);
/*     */     }
/*     */     
/* 120 */     return connPool;
/*     */   }
/*     */   
/*     */   private static LDAPConnectionPool createConnPoolToInMemoryLdapServer(LdapServerConfig config) throws LdapException
/*     */   {
/* 125 */     return InMemoryLdapServer.createConnPool("ZIMBRA_LDAP_SERVER", config);
/*     */   }
/*     */   
/*     */   static void closeAll() {
/* 129 */     for (LDAPConnectionPool connPool : connPools.values()) {
/* 130 */       connPool.close();
/*     */     }
/*     */   }
/*     */   
/*     */   private static synchronized void addToPoolMap(LDAPConnectionPool connPool) {
/* 135 */     String poolName = connPool.getConnectionPoolName();
/* 136 */     LDAPConnectionPool pool = (LDAPConnectionPool)connPools.get(poolName);
/* 137 */     assert (pool == null);
/*     */     
/* 139 */     connPools.put(poolName, connPool);
/*     */   }
/*     */   
/*     */   public static synchronized LDAPConnectionPool getConnPoolByName(String connPoolName) {
/* 143 */     return (LDAPConnectionPool)connPools.get(connPoolName);
/*     */   }
/*     */   
/*     */   private static synchronized LDAPConnectionPool getConnPool(String connPoolName, LdapServerConfig.ExternalLdapConfig config)
/*     */     throws LdapException
/*     */   {
/* 149 */     LDAPConnectionPool pool = (LDAPConnectionPool)connPools.get(connPoolName);
/* 150 */     if (pool == null)
/*     */     {
/* 152 */       pool = createConnectionPool(connPoolName, config);
/*     */     }
/* 154 */     return pool;
/*     */   }
/*     */   
/*     */   static LDAPConnectionPool getConnPoolByConfig(LdapServerConfig.ExternalLdapConfig config) throws LdapException
/*     */   {
/* 159 */     String connPoolName = LdapServerConfig.ExternalLdapConfig.ConnPoolKey.getConnPoolKey(config);
/* 160 */     return getConnPool(connPoolName, config);
/*     */   }
/*     */   
/*     */   @LdapTODO.TODO
/*     */   private static BindRequest createBindRequest(LdapServerConfig config) {
/* 165 */     String bindDN = config.getAdminBindDN();
/*     */     
/* 167 */     if (bindDN != null) {
/* 168 */       String bindPassword = config.getAdminBindPassword();
/* 169 */       return new SimpleBindRequest(bindDN, bindPassword);
/*     */     }
/* 171 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static void debugCheckOut(LDAPConnectionPool connPool, LDAPConnection conn)
/*     */   {
/* 190 */     if (!DebugConnPool.enabled()) {
/* 191 */       return;
/*     */     }
/* 193 */     DebugConnPool.checkOut(connPool, conn);
/*     */   }
/*     */   
/*     */   static void debugCheckIn(LDAPConnectionPool connPool, LDAPConnection conn) {
/* 197 */     if (!DebugConnPool.enabled()) {
/* 198 */       return;
/*     */     }
/* 200 */     DebugConnPool.checkIn(connPool, conn);
/*     */   }
/*     */   
/*     */   @LdapTODO.TODO
/*     */   public static void dump() {
/* 205 */     if (!DebugConnPool.enabled()) {
/* 206 */       return;
/*     */     }
/* 208 */     DebugConnPool.access$200();
/*     */   }
/*     */   
/*     */ 
/*     */   private static class DebugConnPool
/*     */   {
/* 214 */     private static final Map<String, DebugConnPool> checkedOutByPoolName = new HashMap();
/*     */     
/* 216 */     private DebugConnPool() { this.checkedOutConns = new ArrayList(); }
/*     */     
/*     */     static boolean enabled() {
/* 219 */       return LC.ldap_connect_pool_debug.booleanValue();
/*     */     }
/*     */     
/*     */     @LdapTODO.TODO
/*     */     private static void output(String msg) {
/* 224 */       System.out.println(msg);
/*     */     }
/*     */     
/*     */     private void dumpDebugConnPool(long now) {
/* 228 */       output("Number of checked out connections: " + this.checkedOutConns.size() + "\n");
/*     */       
/* 230 */       for (CheckedOutInfo checkedOutConn : this.checkedOutConns)
/* 231 */         checkedOutConn.dump(now); }
/*     */     
/*     */     private static class CheckedOutInfo { long connId;
/*     */       String connPoolName;
/*     */       long checkedOutTimestamp;
/*     */       StackTraceElement[] stackTrace;
/*     */       
/* 238 */       private CheckedOutInfo(LDAPConnection conn) { this.connId = conn.getConnectionID();
/* 239 */         this.connPoolName = conn.getConnectionPoolName();
/* 240 */         this.checkedOutTimestamp = System.currentTimeMillis();
/*     */         
/* 242 */         Thread currentThread = Thread.currentThread();
/* 243 */         this.stackTrace = currentThread.getStackTrace();
/*     */       }
/*     */       
/*     */       private void dump(long now) {
/* 247 */         StringBuilder sb = new StringBuilder();
/* 248 */         sb.append("connId: " + this.connId + " (" + this.connPoolName + ")" + "\n");
/* 249 */         sb.append("elapsed milli secs: " + (now - this.checkedOutTimestamp) + "\n");
/* 250 */         sb.append("\n");
/* 251 */         for (StackTraceElement element : this.stackTrace) {
/* 252 */           sb.append(element.toString() + "\n");
/*     */         }
/* 254 */         sb.append("\n");
/*     */         
/* 256 */         LdapConnectionPool.DebugConnPool.output("--------------------");
/* 257 */         LdapConnectionPool.DebugConnPool.output(sb.toString());
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */     private List<CheckedOutInfo> checkedOutConns;
/*     */     
/*     */ 
/*     */     private static synchronized void checkOut(LDAPConnectionPool connPool, LDAPConnection conn)
/*     */     {
/* 268 */       String connPoolName = connPool.getConnectionPoolName();
/* 269 */       DebugConnPool checkedOutFromPool = (DebugConnPool)checkedOutByPoolName.get(connPoolName);
/*     */       
/* 271 */       if (checkedOutFromPool == null) {
/* 272 */         checkedOutFromPool = new DebugConnPool();
/* 273 */         checkedOutByPoolName.put(connPoolName, checkedOutFromPool);
/*     */       }
/*     */       else {
/* 276 */         for (CheckedOutInfo checkedOutConn : checkedOutFromPool.checkedOutConns) {
/* 277 */           long connId = conn.getConnectionID();
/* 278 */           if (connId == checkedOutConn.connId) {
/* 279 */             output("connection " + connId + " is already checked out.");
/* 280 */             checkedOutConn.dump(System.currentTimeMillis());
/* 281 */             if (!$assertionsDisabled) { throw new AssertionError();
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/* 286 */       CheckedOutInfo checkedOutConn = new CheckedOutInfo(conn, null);
/* 287 */       checkedOutFromPool.checkedOutConns.add(checkedOutConn);
/*     */     }
/*     */     
/*     */     private static synchronized void checkIn(LDAPConnectionPool connPool, LDAPConnection conn) {
/* 291 */       String connPoolName = connPool.getConnectionPoolName();
/* 292 */       DebugConnPool checkedOutFromPool = (DebugConnPool)checkedOutByPoolName.get(connPoolName);
/*     */       
/* 294 */       assert (checkedOutFromPool != null);
/*     */       
/* 296 */       boolean checkedIn = false;
/* 297 */       for (Iterator<CheckedOutInfo> it = checkedOutFromPool.checkedOutConns.iterator(); it.hasNext();) {
/* 298 */         CheckedOutInfo checkedOutConn = (CheckedOutInfo)it.next();
/* 299 */         long connId = conn.getConnectionID();
/* 300 */         if (connId == checkedOutConn.connId) {
/* 301 */           checkedOutFromPool.checkedOutConns.remove(checkedOutConn);
/* 302 */           checkedIn = true;
/* 303 */           break;
/*     */         }
/*     */       }
/*     */       
/* 307 */       assert (checkedIn);
/*     */     }
/*     */     
/*     */     private static synchronized void dump() {
/* 311 */       long now = System.currentTimeMillis();
/* 312 */       for (Map.Entry<String, DebugConnPool> checkedOutFromPool : checkedOutByPoolName.entrySet()) {
/* 313 */         String poolName = (String)checkedOutFromPool.getKey();
/* 314 */         DebugConnPool checkedOutConns = (DebugConnPool)checkedOutFromPool.getValue();
/* 315 */         output("====================");
/* 316 */         output("Pool " + poolName);
/* 317 */         output("====================");
/* 318 */         checkedOutConns.dumpDebugConnPool(now);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/ldap/unboundid/LdapConnectionPool.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */